package com.starblink.android.basic.sensorsdata.extension

import android.content.Context
import android.view.View
import androidx.annotation.FloatRange
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentContainerView
import com.starblink.android.basic.R
import com.starblink.android.basic.base.activity.AbsActivity
import com.starblink.android.basic.base.fragment.AbsFragment
import com.starblink.android.basic.sensorsdata.SkViewTracker
import com.starblink.android.basic.sensorsdata.exposure.config.ExposeConfig
import com.starblink.android.basic.sensorsdata.room.entity.SkAntEntity
import com.starblink.android.basic.sensorsdata.spm.GTrackerAssistUtils


/**
 *  Create by yuanl at 2023/5/5
 **/
/**
 * view埋点扩展
 *
 * @param onlyHit 只有点击
 */
fun View.trackData(
    page: Int,
    element: Int,
    params: Map<String, String>? = null,
    @FloatRange(from = 0.01, to = 1.0) showRatio: Float = ExposeConfig.showRatio,
    exposeTime: Float = ExposeConfig.exposeTime,
    onlyHit: Boolean = false,
    eventArgs: Map<String, String>? = null,
    businessSuffixID: String? = null,
    traceInfo: String? = null
) {
    val entity = SkAntEntity(
        params = params ?: emptyMap(),
        spmCnt = GTrackerAssistUtils.fetchElementValue(
            page,
            element,
        ),
        eventParams = eventArgs ?: mapOf(),
        traceInfo = traceInfo ?: "",
        businessID = if (businessSuffixID != null) "$businessSuffixID$element" else null
    )

    SkViewTracker.trackView(this, entity, showRatio, exposeTime, onlyHit)

}


fun businessSuffixId(any: Any) =
    "${System.identityHashCode(any)}"

fun Any.trackBizSuffixId() = businessSuffixId(this)

//fun businessSuffixId(beanStr: String, id: String?) =
//    "${beanStr}_${id}"

//fun Any.businessId(page: Any, element: Any, beanStr: String, id: String?) =
//    "${page}_${element}_${beanStr}_${id}"

fun Context.findActivitySpmId(): Int = (this as? AbsActivity)?.spmPageId()?:0

fun Context.findActivitySpmParams(): Map<String, String>? = (this as? AbsActivity)?.spmPageParams()

fun View.findFragment(): Fragment? {
    var fragment: Fragment? = null
    var view: View? = this
    while (view != null) {
        val tag = view.getTag(R.id.fragment_root_tag)
        if (tag is Fragment) {
            fragment = tag
            break
        }
        view = (view.parent as? View)
    }
    return fragment
}

/**
 * 获取当前view所属Fragment页面埋点ID
 * 最好缓存，避免多次查找
 */
fun View.findFragmentSpmId(): Int =
    kotlin.runCatching { (this.findFragment() as? AbsFragment)?.spmPageId() }.getOrNull() ?: 0

/**
 * 获取当前view所属Fragment页面埋点数据
 * 最好缓存，避免多次查找
 */
fun View.findFragmentSpmParams(): Map<String, String>? =
    kotlin.runCatching { (this.findFragment() as? AbsFragment)?.spmPageParams() }.getOrNull()

/**
 * 获取当前view所属页面埋点ID
 */
fun View.findParentPageSpmId(): Int = findFragmentSpmId().let {
    if (it == 0) {
        context.findActivitySpmId()
    } else {
        it
    }
}

/**
 * 获取当前view所属页面埋点数据
 */
fun View.findParentPageSpmParams(): Map<String, String>? =
    findFragmentSpmParams() ?: context.findActivitySpmParams()

